<template>
  <div class="app-container">
    用户列表

    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="userQuery.username" placeholder="用户名"/>
      </el-form-item>

      <el-form-item>
        <el-input v-model="userQuery.phone" clearable placeholder="电话">
        </el-input>
      </el-form-item>

      <el-form-item label="添加时间">
        <el-date-picker
          v-model="userQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="userQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="list"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="username" label="用户名"/>

      <el-table-column prop="phone" label="电话" />

      <el-table-column prop="createTime" label="创建时间" :formatter="dateFormat" />

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button circle icon="el-icon-edit-outline" type="primary" title="编辑" size="small"
           @click="userEdit(scope.row.id)"></el-button>
          <el-button circle icon="el-icon-delete" type="danger" title="删除" size="small"
           @click="removeDataById(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="收货地址"
     :visible.sync="userDialog"
     width="430px">
      <el-form :model="userdata">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userdata.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userdata.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="userdata.phone"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="userDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateUserById(userdata)">保 存</el-button>
      </div>
    </el-dialog>
  <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />

  </div>
</template>
<script>
//引入调用user.js文件
import UserApi from '@/api/tourist/user'
//时间戳格式化
import moment from 'moment'
export default {
    //写核心代码位置
    // data:{
    // },
    data() { //定义变量和初始值
        return {
          list:null,//查询之后接口返回集合
          page:1,//当前页
          limit:10,//每页记录数
          total:0,//总记录数
          userQuery:{}, //条件封装对象
          userDialog:false,
          formLabelWidth: '120px',
          userdata:{
             id:"",
             username:"",
             email:"",
             phone:""
          }

        }
    },
    created() { //页面渲染之前执行，一般调用methods定义的方法
        //调用
        this.getList()
    },
    methods:{  //创建具体的方法，调用user.js定义的方法
        //讲师列表的方法
        getList(page=1) {
            this.page = page
            UserApi.getUserListPage(this.page,this.limit,this.userQuery)
                .then(response =>{//请求成功
                    //response接口返回的数据
                    //console.log(response)
                    this.list = response.data.rows
                    this.total = response.data.total
                    console.log(this.list)
                    console.log(this.total)
                })
        },
        dateFormat:function(row, column) {
            var date = row[column.property];
            if (date == undefined) {
                return "";
            }
            return moment(date).format("YYYY-MM-DD HH:mm:ss");
        },
        resetData() {//清空的方法
            //表单输入项数据清空
            this.userQuery = {}
            //查询所有讲师数据
            this.getList()
        },
        //删除用户的方法
        removeDataById(id) {
            this.$confirm('此操作将永久删除用户记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {  //点击确定，执行then方法
                //调用删除的方法
                UserApi.deleteUserId(id)
                    .then(response =>{//删除成功
                    //提示信息
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    //回到列表页面
                    this.getList()
                })
            }) //点击取消，执行catch方法
        },
        userEdit(id) {
          this.userDialog = true
          UserApi.getUserById(id).then(response =>{
            this.userdata = response.data.user
          })
        },
        updateUserById(userdata){
           this.userDialog = false
           UserApi.updateUser(userdata).then(response =>{
             //提示信息
             this.$message({
                 type: 'success',
                 message: '保存成功!'
             });
             //回到列表页面
             this.getList()
           })
        }

    }
}
</script>
